/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.mixin.exploit;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.Packet;
import net.minecraft.network.play.client.CPacketCreativeInventoryAction;
import net.minecraft.util.text.TextComponentString;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.common.SpongeImpl;
import org.spongepowered.common.network.PacketUtil;
import org.spongepowered.common.util.SpongeHooks;

@Mixin(value = PacketUtil.class, remap = false)
public class MixinPacketUtil {

//    private static final String EXPLOIT_RETURN_INJECTION = "Lorg/spongepowered/common/util/StaticMixinHelper;packetPlayer:"
//                                                           + "Lnet/minecraft/entity/player/EntityPlayerMP;";

    private static boolean isExploit = false;

    /**
     * @author gabizou
     * @reason Overwrites our sanity check for catching an exploited item stack.
     * @return true if exploit detected
     */
    @Overwrite
    private static boolean creativeExploitCheck(Packet<?> packet, EntityPlayerMP playerMP) {
        if (!(packet instanceof CPacketCreativeInventoryAction)) {
            return false;
        }
        if (!SpongeImpl.getGlobalConfig().getConfig().getExploits().isPreventItemNameOverflow()) {
            return false;
        }
        // Fix string overflow exploit in creative mode
        CPacketCreativeInventoryAction creativePacket = (CPacketCreativeInventoryAction) packet;
        ItemStack itemstack = creativePacket.getStack();
        if (!itemstack.isEmpty() && itemstack.getDisplayName().length() > 32767) {
            SpongeHooks.logExploitItemNameOverflow(playerMP, itemstack.getDisplayName().length());
            playerMP.connection.disconnect(new TextComponentString("You have been kicked for attempting to perform an itemstack name overflow exploit."));
            isExploit = true;
            return true;
        }
        return false;
    }

//    @Inject(method = "onProcessPacket", at = @At(value = "FIELD", target = EXPLOIT_RETURN_INJECTION, opcode = Opcodes.GETSTATIC, args = "log=true"),
//            cancellable = true, remap = false)
//    private static void onExploitCheck(CallbackInfo callbackInfo) {
//        if (isExploit) {
//            isExploit = false;
//            PacketUtil.resetStaticData();
//            callbackInfo.cancel();
//        }
//    }

}
